package cn.edu.zafu.bigdata.mahuilin;

import java.io.*;
import java.net.*;

public class TimeClient {
    public static void main(String[] args) throws IOException {
        String hostname = "localhost"; // 服务端的主机名或IP地址
        int port = 1234; // 服务端监听的端口号

        try (DatagramSocket clientSocket = new DatagramSocket()) {
            String sentence = "Current time, please";
            byte[] sendData = sentence.getBytes();
            byte[] receiveData = new byte[1024];

            InetAddress IPAddress = InetAddress.getByName(hostname);
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
            clientSocket.send(sendPacket);

            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            clientSocket.receive(receivePacket);
            String response = new String(receivePacket.getData(), 0, receivePacket.getLength());
            System.out.println("Server says: " + response);
        } catch (UnknownHostException e) {
            System.out.println("Server not found: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("I/O Error: " + e.getMessage());
        }
    }
}
